import 'package:fluent_ui/fluent_ui.dart';
import 'package:flutter/foundation.dart';
import 'package:fnos_tv_desktop/core/api/bean/media_db_result.dart';

class MediaDatabases extends ChangeNotifier {
  List<MediaDatabase>? databases;

  void update(List<MediaDbResult> dbs, Map<String, dynamic> sums) {
    databases = dbs.map((e) => MediaDatabase(e, sums[e.guid] as int?)).toList();
    notifyListeners();
  }

  void updateSum(Map<String, dynamic> sums) {
    if (databases != null) {
      databases = databases!
          .map((e) => e.copyWith(sum: sums[e.guid] as int? ?? 0))
          .toList();
      notifyListeners();
    }
  }

  void clear() {
    databases = null;
  }
}

class MediaDatabase {
  final String guid;
  final String title;
  final String category;
  final List<String> posters;
  final int viewType;
  final int sum;

  MediaDatabase._({
    this.guid = '',
    this.title = '',
    this.category = '',
    this.posters = const [],
    this.viewType = 0,
    this.sum = 0,
  });

  MediaDatabase(MediaDbResult e, int? sum)
    : guid = e.guid,
      title = e.title,
      category = e.category,
      posters = e.posters,
      viewType = e.viewType,
      sum = sum ?? 0;

  MediaDatabase copyWith({int? sum}) {
    return MediaDatabase._(
      guid: guid,
      title: title,
      category: category,
      posters: posters,
      viewType: viewType,
      sum: sum ?? this.sum,
    );
  }

  @override
  operator ==(Object other) =>
      other is MediaDatabase &&
      other.guid == guid &&
      other.title == title &&
      other.category == category &&
      other.posters == posters &&
      other.viewType == viewType &&
      other.sum == sum;

  @override
  int get hashCode =>
      Object.hash(guid, title, category, posters, viewType, sum);

  @override
  String toString() {
    return 'MediaDatabase{guid: $guid, title: $title, category: $category, posters: $posters, viewType: $viewType, sum: $sum}';
  }
}
